const Controller = require('egg').Controller;
class LoginController extends Controller {
    async login() {
        let username = this.ctx.request.body;
        let data = await this.ctx.service.login.Login(username);
        console.log(data);
        if (data.data != null && data.data != undefined && data.data != '') {
            let token = this.app.jwt.sign(username.name,this.app.config.jwt.secret);
            this.ctx.body = {
                code: 200,
                msg: '登录成功',
                token: token
            }
        } else {
            this.ctx.body = {
                code: 502,
                msg: '用户名或密码错误',
                token: ''
            }
        }
    }
    async create() {
        let username = this.ctx.request.body;
        let data=await this.ctx.service.login.create(username);
        data.data=await this.app.model.Username.findAll();
        this.ctx.body = data;
    }
    async update(){
        let username = this.ctx.request.body;
        let data=await this.ctx.service.login.update(username);
        if(data.code == 200){
             data.data=await this.app.model.Username.findAll()
             this.ctx.body=data
        }else{
            this.ctx.body=data
        }
        
    }
    async destroy(){
        let username = this.ctx.params.id;
        let data=await this.ctx.service.login.destroy(username);
        this.ctx.body=data;
    }
    async index(){
        try{
            this.ctx.body = {
                code:200,
                msg:'查询成功',
                data:await this.app.model.Username.findAll()
            }
        }catch{
            this.ctx.body = {
                code:501,
                msg:'查询失败'
            }
        }
    }
}
module.exports = LoginController;